package com.bss.controller;

import com.bss.pojo.Customer;
import com.bss.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:49
 */
@RestController
@RequestMapping("/bss/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @RequestMapping("find")
    public Customer getCustomer(@RequestParam("id") Integer id){
        return customerService.findCustomerById(id);
    }
    @RequestMapping("addOrUpd")
    public void addOrUpd(Customer customer){
        System.out.println(customer);
        if ((customer.getCustomerId()!= null)){
            customerService.updateCustomer(customer);
        }else {
            customerService.addCustomer(customer);
        }
    }
    @RequestMapping("del")
    public void delCustomer(@RequestParam("id") Integer id){
        customerService.deleteCustomerById(id);
    }
    @RequestMapping("getAll")
    public PageInfo<Customer> getAll(Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Customer> customerList = customerService.findAll();
        PageInfo<Customer> customerPageInfo = new PageInfo<Customer>(customerList);
        return customerPageInfo;
    }
    @RequestMapping("query")
    public PageInfo<Customer> query(String customerName,String customerAddr){
        PageHelper.startPage(1,5);
        List<Customer> customer = customerService.findCustomer(customerName, customerAddr);
        PageInfo customerPageInfo = new PageInfo(customer);
        return customerPageInfo;
    }
}
